package cn.cdeden.knowledge.utils;

import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.FilesSynergy;
import cn.cdeden.knowledge.domain.FilesSynergyUser;
import cn.cdeden.knowledge.domain.dto.SynergyFileUserEnums;
import cn.cdeden.knowledge.domain.dto.UserSynergyQueryParam;
import cn.cdeden.knowledge.domain.vo.FilesSynergyVo;
import cn.cdeden.knowledge.mapper.FilesSynergyMapper;
import cn.cdeden.knowledge.service.FilesSynergyUserService;
import cn.cdeden.system.api.model.LoginUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
@Slf4j
public class FilesSynergyUtils {

    private final FilesSynergyMapper synergyMapper;

    private final FilesSynergyUserService synergyUserService;


    public TableDataInfo<FilesSynergyVo> queryUserInfo(UserSynergyQueryParam query, Pageable pageable){
        IPage<FilesSynergy> queryPage = PageUtil.toMybatisPage(pageable);
        query.setUserId(String.valueOf(LoginHelper.getUserId()));
//        query.setUserId(String.valueOf(FileUserUtils.getUser().getId()));
        IPage<FilesSynergyVo> page = synergyMapper.queryUserInfo(queryPage, query);
        return TableDataInfo.build(page);
    }


    public boolean deleteUserInfo(String synergyId) {
        LoginUser user = LoginHelper.getLoginUser();
//        FileUserDto user = FileUserUtils.getUser();
        LambdaQueryWrapper<FilesSynergyUser> wrapper = new LambdaQueryWrapper<FilesSynergyUser>()
            .eq(FilesSynergyUser::getSynergyId, synergyId);
        FilesSynergyVo filesSynergyVo = queryById(synergyId, String.valueOf(user.getUserId()));
        String operation = filesSynergyVo.getOperation();
        if (StringUtils.equals(SynergyFileUserEnums.Owner.getCode(),operation)){ //suo
            synergyMapper.deleteById(synergyId);
            //TODO 发送消息待补充
//            messageSend(SynergyMessageSendEnums.delete,null,synergyId,null); //发送消息
        }else {
            wrapper.eq(FilesSynergyUser::getUserId, user.getUserId());
        }
        return synergyUserService.remove(wrapper);
    }


    public FilesSynergyVo queryById(String id,String userId) {
        FilesSynergyVo info = synergyMapper.queryById(id,userId);
        return info;
    }



}
